//
//  VideoViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/5.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoSubViewController.h"
#import "JXCategoryView.h"
#import "JXCategoryListContainerView.h"
#import "JXCategoryTitleView.h"
@interface VideoViewController ()<JXCategoryListContainerViewDelegate, JXCategoryViewDelegate>
@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) JXCategoryBaseView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@property (nonatomic, assign) BOOL isNeedIndicatorPositionChangeItem;

@property (nonatomic, strong) JXCategoryTitleView *myCategoryTitleView;

@property (nonatomic, assign)  NSInteger page;

@property (nonatomic, strong) NSArray *navList; 
@end

@implementation VideoViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setIfRefreshFirst:(BOOL)ifRefreshFirst
{
    _ifRefreshFirst = ifRefreshFirst;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categorySelectToIndex:) name:@"yingxiangSelect" object:nil];
    self.page = 1;
    [self setupUI];
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categorySelectToIndex:) name:@"yingxiangSelect" object:nil];
    self.page = 1;
    [self setupUI];
    [self requestData];
}

- (void)categorySelectToIndex:(NSNotification *)noti
{
    NSDictionary *dataDic = noti.userInfo;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.categoryView selectItemAtIndex:[dataDic[@"selectID"] integerValue]];
    });
}

- (void)requestData
{
    NSDictionary *dict = @{@"page":@(self.page),
                           @"limit":@"10"
    };
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"sort/1" parameters:dict done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        self.navList = responseObject[@"data"][@"nav_list"];
        NSMutableArray *titleList = [NSMutableArray array];
        for (NSDictionary *menu in responseObject[@"data"][@"nav_list"]) {
            [titleList addObject:menu[@"title"]];
        }
        self.titles = [titleList copy];
        
        self.myCategoryTitleView.titles = self.titles;
        [self.view addSubview:self.listContainerView];
        self.categoryView.listContainer = self.listContainerView;
        self.categoryView.delegate = self;
        [self.view addSubview:self.categoryView];
        
        self.myCategoryTitleView.titleFont = JSS_MEDIUM_FONT_4;
        self.isNeedIndicatorPositionChangeItem = YES;
        self.myCategoryTitleView.titleColorGradientEnabled = YES;
        JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
        lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
        //可以试试宽度补偿
        lineView.indicatorWidthIncrement = 10;
        self.myCategoryTitleView.indicators = @[lineView];
        
//        [self.categoryView selectItemAtIndex:0];
//        [self.myCategoryTitleView selectItemAtIndex:2];
//        [self.categoryView selectItemAtIndex:0];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

- (void)setupUI
{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = RGB_VALUE(0xDC0000);
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(Height_NavBar);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"影像馆";
    titleLabel.font = JSS_MEDIUM_FONT(17);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-9);
        make.centerX.offset(0);
        make.width.mas_equalTo(135);
        make.height.mas_equalTo(24);
    }];
    
     [self myCategoryTitleView];
    [self preferredCategoryView];
}

- (JXCategoryTitleView *)myCategoryTitleView {
    return (JXCategoryTitleView *)self.categoryView;
}

- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.categoryView.frame = CGRectMake(0, Height_NavBar, self.view.bounds.size.width, [self preferredCategoryViewHeight]);
    self.listContainerView.frame = CGRectMake(0, [self preferredCategoryViewHeight]+Height_NavBar, self.view.bounds.size.width, self.view.bounds.size.height - [self preferredCategoryViewHeight]-Height_NavBar);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    //处于第一个item的时候，才允许屏幕边缘手势返回
    self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    //离开页面的时候，需要恢复屏幕边缘手势，不能影响其他页面
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

- (CGFloat)preferredCategoryViewHeight {
    return 50;
}

- (JXCategoryBaseView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [self preferredCategoryView];
    }
    return _categoryView;
}

- (JXCategoryListContainerView *)listContainerView {
    if (_listContainerView == nil) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

#pragma mark - JXCategoryListContainerViewDelegate

-(id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    VideoSubViewController *list = [[VideoSubViewController alloc] init];
    list.navDic = self.navList[index];
    return list;
}

-(NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    //侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}


@end
